package cn.me.Lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * 消费型接口 Consumer<T> void accept(T t) 
 * 供给型接口 Supplier<T> T get() 
 * 函数型接口 Function<T, R> R apply(T t) 
 * 断定型接口 Perdicate<T> boolean test(T t)
 */
public class Lambda2 {
    public static void main(String[] args) {
        happyTime(500, new Consumer<Double>() {
            @Override
            public void accept(Double t) {
                System.out.println("学习太累了，去天上人间买了瓶矿泉水，价格为" + t);                
            }
        });

        happyTime(400, money -> System.out.println("学习太累了，去天上人间买了瓶矿泉水，价格为" + money));
        System.out.println("**************************************");

        List<String> list = Arrays.asList("北京", "南京", "天津", "东京", "西京", "普京");
        list = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        for(var l : list) {
            System.out.print(l + " ");
        }
        System.out.println();

        list = filterString(list, s -> s.contains("京"));
        for(var l : list) {
            System.out.print(l + " ");
        }

    }

    public static void happyTime(double money, Consumer<Double> con) {
        con.accept(money);
    }

    // 根据给定的规则过滤集合中的字符串，规则由Predicate的方法决定
    public static List<String> filterString(List<String> list, Predicate<String> pre) {
        List<String> filterList = new ArrayList<>();
        for(var s : list) {
            if(pre.test(s)) {
                filterList.add(s);
            }
        }

        return filterList;
    }


}
